package com.jungle.task;

import com.jungle.task.executor.CompensateTaskExecutor;
import com.jungle.task.executor.TaskExecutor;
import com.jungle.task.parameter.CompensateTaskParameter;
import com.jungle.task.result.CompensateTaskResult;

import java.util.List;

/**
 * @author 李岩 at 2021/10/23.
 */
public class CompensableTaskDispatcher extends TaskDispatcher{
    private final CompensateTaskExecutor compensateTaskExecutor;

    public CompensableTaskDispatcher(final List<TaskExecutor<?, ?>> executors) {
        super(executors);
        this.compensateTaskExecutor =
            (CompensateTaskExecutor)super.filter(executors,super.predicate(TaskType.COMPENSATE)).findFirst().orElse(null);
    }

    @Override
    public CompensateTaskResult dispatchCompensateTask(final Task<CompensateTaskParameter, CompensateTaskResult> task) {
        return this.compensateTaskExecutor.execute(task);
    }
}
